import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '@/stores/authStore'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/pages/auth/LoginPage.vue'),
    meta: { requireAuth: false, title: '登录' }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/components/layout/AppLayout.vue'),
    redirect: '/dashboard',
    meta: { requireAuth: true },
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import('@/pages/dashboard/Dashboard.vue'),
        meta: { title: '监控大盘', icon: 'DashboardOutlined' }
      },
      {
        path: '/alerts',
        name: 'AlertCenter',
        component: () => import('@/pages/alerts/AlertCenter.vue'),
        meta: { title: '告警中心', icon: 'BellOutlined' }
      },
      {
        path: '/tickets',
        name: 'TicketSystem',
        component: () => import('@/pages/tickets/TicketSystem.vue'),
        meta: { title: '工单系统', icon: 'FormOutlined' }
      },
      {
        path: '/tickets/:id',
        name: 'TicketDetail',
        component: () => import('@/pages/tickets/TicketDetail.vue'),
        meta: { title: '工单详情', icon: 'FormOutlined' }
      },
      {
        path: '/knowledge',
        name: 'Knowledge',
        component: () => import('@/pages/knowledge/Knowledge.vue'),
        meta: { title: '知识库', icon: 'BookOutlined' }
      },
      {
        path: '/analytics',
        name: 'Analytics',
        component: () => import('@/pages/analytics/Analytics.vue'),
        meta: { title: '数据分析', icon: 'BarChartOutlined' }
      },
      {
        path: '/settings',
        name: 'Settings',
        component: () => import('@/pages/settings/Settings.vue'),
        meta: { title: '系统设置', icon: 'SettingOutlined' }
      },
      {
        path: '/system',
        name: 'SystemStatus',
        component: () => import('@/pages/system/SystemStatus.vue'),
        meta: { title: '系统状态', icon: 'MonitorOutlined' }
      },
      {
        path: '/users',
        name: 'Users',
        component: () => import('@/pages/users/Users.vue'),
        meta: { title: '用户管理', icon: 'UserOutlined' }
      },
      {
        path: '/notifications',
        name: 'Notifications',
        component: () => import('@/pages/notifications/Notifications.vue'),
        meta: { title: '通知中心', icon: 'BellOutlined' }
      },
      {
        path: '/monitor',
        name: 'RealTimeMonitor',
        component: () => import('@/pages/monitor/RealTimeMonitor.vue'),
        meta: { title: '实时监控', icon: 'LineChartOutlined' }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/pages/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  const requireAuth = to.meta.requireAuth !== false

  if (requireAuth && !authStore.isAuthenticated) {
    next({ name: 'Login', query: { redirect: to.fullPath } })
  } else if (to.name === 'Login' && authStore.isAuthenticated) {
    next({ name: 'Dashboard' })
  } else {
    next()
  }
})

export default router
